package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import com.sdk.mobile.manager.login.NoDoubleClickListener;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemLeaseOrderStateBinding;
import com.sgb.kjwl.model.entity.lease.LeaseOrderListEntity;
import com.sgb.kjwl.view.widget.lease.LeaseEditLogisticsDialog;
import com.swgk.core.dialog.BaseBottomDialog;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.text.DecimalFormat;

/**
 * 出租订单itme
 */
public class LeaseOrderStateAdapter extends BaseRecycleAdapter<LeaseOrderListEntity> {
    private Context context;
    private ItemCallBack itemCallBack;
    private String type = "1";//1=租入,2=租出
    ItemLeaseOrderStateBinding binding;

    public LeaseOrderStateAdapter(Context context, String type) {
        super(context, R.layout.item_lease_order_state, null);
        this.context = context;
        this.type = type;
    }

    @Override
    public void convert(ItemViewHolder holder, final LeaseOrderListEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        binding = (ItemLeaseOrderStateBinding) holder.getmBinding();
        binding.tvIsSelf.setVisibility("1".equals(entity.getIsSelf()) ? View.VISIBLE : View.GONE);//是否自营
        binding.tvOrderTotal.setText("￥" + entity.getReceiveOderPice());
        //出租方是企业时显示企业名称，出租方式是个人时显示个人姓名
        if ("1".equals(type)) {//1为租入列表,判断卖家企业名称是否存在
            binding.tvTypeName.setText("出租方：");
            if (!TextUtils.isEmpty(entity.getSellerCompanyName())) {
                binding.tvCompanyName.setText(entity.getSellerCompanyName());
            } else {
                binding.tvCompanyName.setText(entity.getSellerUserName());
            }
        } else {//2为租出列表,判断买家企业名称是否存在
            binding.tvTypeName.setText("承租方：");
            if (!TextUtils.isEmpty(entity.getBuyerCompanyName())) {
                binding.tvCompanyName.setText(entity.getBuyerCompanyName());
            } else {
                binding.tvCompanyName.setText(entity.getBuyerUserName());
            }
        }

        try {
            String rentalPrice = entity.getRentalPrice();
            double rentalPriceInt = Double.valueOf(rentalPrice);
            if (rentalPriceInt <= 0) {
                binding.tvPirce.setText("面议");
                binding.tvRateText.setVisibility(View.GONE);
            } else {
                binding.tvPirce.setText(entity.getRentalPrice() + entity.getRentalPriceUnitName());
                binding.tvRateText.setVisibility(View.VISIBLE);
            }
            if (!TextUtils.isEmpty(entity.getNewRentalPrice())) {//如果价格已被修改，则显示，为空则不显示
                binding.tvPirce.setText(entity.getNewRentalPrice() + entity.getRentalPriceUnitName());
            }
        } catch (Exception e) {
            binding.tvPirce.setText("面议");
            binding.tvRateText.setVisibility(View.GONE);
        }


        if (TextUtils.isEmpty(entity.getNewRentalPrice())) {//是否有改价
            if ("1".equals(entity.getHaveRate())) {
                if ("1".equals(entity.getHaveDriver())) {
                    binding.tvRateText.setText("(含驾驶员、含税)");
                } else {
                    binding.tvRateText.setText("(不含驾驶员、含税)");
                }
            } else {
                if ("1".equals(entity.getHaveDriver())) {
                    binding.tvRateText.setText("(含驾驶员、不含税)");
                } else {
                    binding.tvRateText.setText("(不含驾驶员、不含税)");
                }
            }
        } else {
            // driverProvider;//驾驶员提供方 ：1求租方 2出租方
            // buyerNeedInvoice;//买家是否需要开发票：0否 1是
            if ("2".equals(entity.getDriverProvider())) {
                if ("1".equals(entity.getBuyerNeedInvoice())) {
                    binding.tvRateText.setText("(含驾驶员、含税)");
                } else {
                    binding.tvRateText.setText("(含驾驶员、不含税)");
                }
            } else {
                if ("1".equals(entity.getBuyerNeedInvoice())) {
                    binding.tvRateText.setText("(不含驾驶员、含税)");
                } else {
                    binding.tvRateText.setText("(不含驾驶员、不含税)");
                }
            }
        }

        if("2".equals(entity.getModule())){//如果是设备需要将驾驶员文字为操作员
            String haveDriverStr = binding.tvRateText.getText().toString().replace("驾驶员","操作员");
            binding.tvRateText.setText(haveDriverStr);
        }

        //1待接单,2 待确认,3待付款，4支付中，5待运输，6待验收,7已完成,8已取消 ,9已拒绝
        binding.tvConfirm.setVisibility(View.GONE);
        binding.tvDelete.setVisibility(View.GONE);
        binding.tvStatus.setTextColor(Color.parseColor("#969696"));
        String statusText = "";
        if ("1".equals(entity.getStatus())) {
            statusText = "待接单";
            if ("1".equals(type)) {
                binding.tvOrderTotal.setText("￥" + getOderPrice(entity));
//                binding.tvDetail.setVisibility(View.VISIBLE);
                binding.tvConfirm.setVisibility(View.GONE);
            } else {
                binding.tvConfirm.setText("立即接单");
                binding.tvConfirm.setVisibility(View.VISIBLE);
//                binding.tvDetail.setVisibility(View.GONE);
            }
        } else if ("2".equals(entity.getStatus())) {
            statusText = "待确认";
            if ("1".equals(type)) {
                binding.tvConfirm.setText("确认订单");
                binding.tvConfirm.setVisibility(View.VISIBLE);
            }
//            binding.tvDetail.setVisibility(View.GONE);
        } else if ("3".equals(entity.getStatus())) {
            statusText = "待付款";
            if ("1".equals(type)) {
                binding.tvConfirm.setText("申请付款");
                binding.tvConfirm.setVisibility(View.VISIBLE);
            }
//            binding.tvDetail.setVisibility(View.GONE);
        } else if ("4".equals(entity.getStatus())) {
            statusText = "支付中";
            if ("2".equals(type)) {
                statusText = "待付款";
            }
        } else if ("5".equals(entity.getStatus())) {
            statusText = "待运输";
            binding.tvOrderTotalLabel.setText("实付金额：");//将"应付金额"文字描述改为"实付金额"
            if ("2".equals(type)) {
                binding.tvConfirm.setText("立即运输");
                binding.tvConfirm.setVisibility(View.VISIBLE);
//                binding.tvDetail.setVisibility(View.GONE);
            }
        } else if ("6".equals(entity.getStatus())) {
            statusText = "待验收";
            binding.tvOrderTotalLabel.setText("实付金额：");//将"应付金额"文字描述改为"实付金额"
            if ("1".equals(type)) {
                binding.tvConfirm.setText("验收合格");
                binding.tvConfirm.setVisibility(View.VISIBLE);
            }
        } else if ("7".equals(entity.getStatus())) {
            statusText = "已完成";
            binding.tvOrderTotalLabel.setText("实付金额：");//将"应付金额"文字描述改为"实付金额"
            binding.tvDelete.setVisibility(View.VISIBLE);
        } else if ("8".equals(entity.getStatus())) {//8已取消 =交易关闭
            binding.tvStateRefuse.setText("承租方取消订单");
            statusText = "交易关闭";
            binding.tvStatus.setTextColor(Color.parseColor("#ffc000"));
            binding.tvDelete.setVisibility(View.VISIBLE);
        } else if ("9".equals(entity.getStatus())) {//9拒绝 =交易关闭
            binding.tvStateRefuse.setText("出租方拒绝接单");
            statusText = "交易关闭";
            binding.tvStatus.setTextColor(Color.parseColor("#ffc000"));
            binding.tvDelete.setVisibility(View.VISIBLE);
        } else if ("10".equals(entity.getStatus())) {//10待签约
            statusText = "待签约";
        }
        binding.tvStatus.setText(statusText);

        //审核状态下的显示
        if ("1".equals(type)) {//租入订单(待确认,待付款 时需要显示审核状态)
            if ("2".equals(entity.getStatus()) || "3".equals(entity.getStatus())) {
                if ("3".equals(entity.getApprovalStatus())) {
                    binding.tvStatus.setText(Html.fromHtml(statusText + "<font color='#FF4B10'>(已拒绝)</font>"));
                    binding.tvConfirm.setVisibility(View.GONE);
//                    binding.tvDetail.setVisibility(View.VISIBLE);
                } else if ("1".equals(entity.getApprovalStatus())) {
                    binding.tvStatus.setText(Html.fromHtml(statusText + "<font color='#5792FD'>(审批中)</font>"));
                    binding.tvConfirm.setVisibility(View.GONE);
//                    binding.tvDetail.setVisibility(View.VISIBLE);
                }
            }
        } else {//租出订单(待接单 时需要显示审核状态)
            if ("1".equals(entity.getStatus())) {
                if ("3".equals(entity.getApprovalStatus())) {
                    binding.tvStatus.setText(Html.fromHtml(statusText + "<font color='#FF4B10'>(已拒绝)</font>"));
                    binding.tvConfirm.setVisibility(View.GONE);
//                    binding.tvDetail.setVisibility(View.VISIBLE);
                } else if ("1".equals(entity.getApprovalStatus())) {
                    binding.tvStatus.setText(Html.fromHtml(statusText + "<font color='#5792FD'>(审批中)</font>"));
                    binding.tvConfirm.setVisibility(View.GONE);
//                    binding.tvDetail.setVisibility(View.VISIBLE);
                }
            }

        }

        binding.tvDelete.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (itemCallBack != null) {
                    itemCallBack.onDelete(entity.getOrderNo());
                }
            }
        });
        binding.tvConfirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (itemCallBack != null) {
                    itemCallBack.detail(entity.getOrderNo(),entity.getModule());
                }
            }
        });
        binding.llItem.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (itemCallBack != null) {
                    itemCallBack.detail(entity.getOrderNo(),entity.getModule());
                }
            }
        });
    }

//    private void setLeftDrawable(TextView view, int resId) {
//        Drawable drawable = context.getResources().getDrawable(resId);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        view.setCompoundDrawables(drawable, null, null, null);
//    }

    public void setItemCallBack(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public interface ItemCallBack {
        void detail(String id,String module);

        void onDelete(String id);
    }

    private String getOderPrice(LeaseOrderListEntity entity) {
        try {
            //单价*数量*时长+运费
            double oderPrice = Double.parseDouble(entity.getRentalPrice())
                    * Double.parseDouble(entity.getRentalNum())
                    * Double.parseDouble(entity.getRentalTime());
            if (Double.parseDouble(entity.getRentalPrice()) <= 0) {//单价小于0为面议
                return "0.00";
            }
            if (oderPrice <= 0) {
                return "0.00";
            }
            return new DecimalFormat("#0.00").format(oderPrice);
        } catch (Exception exception) {
        }
        return entity.getRentalTotalPrice();

    }

//    //物流信息编辑对话框
//    public void showEditDialog() {
//        LeaseEditLogisticsDialog logisticsDialog = new LeaseEditLogisticsDialog("", "", "", (Activity) context, null);
//        logisticsDialog.setDialogClickCallBack(new LeaseEditLogisticsDialog.DialogClickCallBack() {
//            @Override
//            public void onItemClick(String name, String phone, String carNumber, String reason) {
//
//            }
//        });
//        logisticsDialog.show();
//    }
}
